package com.zlll.winner.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.business.model.media.MediaNewList;
import com.zlll.winner.business.service.media.IMediaListenUserLogService;
import com.zlll.winner.business.service.media.IMediaNewListService;
import com.zlll.winner.business.vo.media.MediaNewListVo;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.enums.media.MediaEnum;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("api/media/new/list")
@RestController
public class MediaNewListController extends BaseController {


    @Reference(version = "${dubbo.consumer.version}")
    private IMediaNewListService mediaNewListService;

    @Reference(version = "${dubbo.consumer.version}")
    private IMediaListenUserLogService listenUserLogService;


    /**
     * 进入直播间
     * @param params
     * @return
     */
    @PostMapping("listen/in")
    public Object listenIn(@RequestBody JSONObject params){
        Long userId = params.getLong("userId");
        Object result = mediaNewListService.livingMediaNewList();
        if(userId !=null && result != null){
            MediaNewListVo listVo = (MediaNewListVo)result;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userId",userId);
            jsonObject.put("newListId",listVo.getId());
            jsonObject.put("type", MediaEnum.LogTypeEnum.IN.val());
            listenUserLogService.delaySaveLog(jsonObject);
        }
        return result == null? getEmptyResult(): result;
    }

    /**
     * 离开直播间
     * @param params
     * @return
     */
    @PostMapping("listen/out")
    public Object listenOut(@RequestBody JSONObject params){
        Long userId = params.getLong("userId");
        Long newListId = params.getLong("newListId");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId",userId);
        jsonObject.put("newListId",newListId);
        jsonObject.put("type", MediaEnum.LogTypeEnum.OUT.val());
        listenUserLogService.delaySaveLog(jsonObject);

        Object result = new JSONObject();
        return result == null? getEmptyResult(): result;
    }

    /**
     * 获取直播节目列表
     * @param params
     * @return
     */
    @PostMapping("today")
    public Object getTodayNewList(@RequestBody JSONObject params){
        return mediaNewListService.fetchNewListWeApp(params);
    }

    /**
     * 直播首页接口
     * @param params
     * @return
     */
    @PostMapping("index")
    public Object getIndexData(@RequestBody JSONObject params){
        JSONObject result = new JSONObject();
        Object livingShow = mediaNewListService.livingMediaNewList();
        result.put("livingShow",livingShow);

        JSONObject fetchParams = new JSONObject();
        fetchParams.put("pageNo",1);
        fetchParams.put("pageSize",2);
        fetchParams.put("status",MediaEnum.StatusEnum.REPLAY.val());
        IPage<MediaNewList> page = mediaNewListService.fetchNewList(fetchParams);
        result.put("replayData",page.getRecords());

        return result;
    }


    /**
     * 听精彩回放分页列表
     * @param params
     * @return
     */
    @PostMapping("replay/page")
    public Object getReplayPage(@RequestBody JSONObject params){
        params.put("status",MediaEnum.StatusEnum.REPLAY.val());
        return mediaNewListService.fetchNewList(params);
    }

    /**
     * 当前直播和等待直播接口
     * @param params
     * @return
     */
    @PostMapping("living-wait")
    public Object getLivingAndWaitData(@RequestBody JSONObject params){
        JSONObject result = new JSONObject();
        Object livingShow = mediaNewListService.livingMediaNewList();
        result.put("livingShow",livingShow);

        JSONObject fetchParams = new JSONObject();
        fetchParams.put("pageNo",1);
        fetchParams.put("pageSize",2);
        fetchParams.put("status",MediaEnum.StatusEnum.WAIT.val());
        IPage<MediaNewList> page = mediaNewListService.fetchNewList(fetchParams);
        result.put("waitData",page.getRecords());

        return result;
    }



}


